<template>
  <div class="list-table">
    <div class=" area-header">
      <span class="area-header-title">{{ $t('模型列表') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('模型名称')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('模型名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('模型标识')" prop="key">
        <el-input
          v-model="queryParams.key"
          :placeholder="$t('模型标识')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>

      <el-form-item :label="$t('模型类型')" prop="category">
        <dictSelect
          v-model="queryParams.category"
          :clearable="true"
          dictionary="act_model_type"
          :placeholder="$t('请选择')"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['act:model:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['act:model:edit']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          :disabled="canExport"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['act:model:edit']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >{{ $t('导入') }}</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="actModelList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center" />
      <el-table-column :label="$t('模型名称')" align="center" prop="name" />
      <el-table-column :label="$t('模型标识')" align="center" prop="key" />
      <el-table-column :label="$t('模型类型')" align="center" prop="category" :formatter="categoryFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('版本号')" align="center" prop="version" show-overflow-tooltip width="100" />
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('更新时间')" align="center" prop="lastUpdateTime" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="" width="400">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['act:model:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row,false)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['act:model:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>

          <el-button
            v-hasPermi="['act:model:edit']"
            size="small"
            type="text"
            icon="el-icon-edit-outline"
            @click="designHandle(scope.row)"
          >{{ $t('设计') }}</el-button>

          <el-button
            v-hasPermi="['act:model:edit']"
            size="small"
            type="text"
            icon="el-icon-success"
            @click="deployHandle(scope.row)"
          >{{ $t('部署') }}</el-button>

          <el-button
            v-hasPermi="['act:model:edit']"
            size="small"
            type="text"
            icon="el-icon-success"
            @click="updateHandle(scope.row,true)"
          >{{ $t('复制') }}</el-button>

          <el-button
            v-hasPermi="['act:model:edit']"
            size="small"
            type="text"
            icon="el-icon-edit-outline"
            @click="msgTemplateHandle(scope.row)"
          >{{ $t('待办提醒') }}</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--修改页面-->
    <el-dialog :title="$t('模型')" :visible.sync="open" width="70%" :close-on-click-modal="false" :lock-scroll="false">
      <actModelForm :act-model-data="actModelData" :copy-model="copyModel" />
    </el-dialog>

    <!--流程导入窗口-->
    <el-dialog :title="$t('模型导入')" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".zip"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          {{ $t('将文件拖到此处，或') }}
          <em>{{ $t('上传') }}</em>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:red"> {{ $t('提示：仅允许导入系统中导出的zip文件') }}</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>

    <!--修改页面-->
    <el-dialog :title="$t('通知模板')" :visible.sync="actModelMsgTemplateOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <actModelMsgTemplateForm ref="actModelMsgTemplateForm" />
    </el-dialog>
  </div>
</template>

<script>
  // 引入和后台交互的JS
  import { getModel, listModel, delModel, deployModel } from '@/api/act/model/actModel'
  import { downLoadFilePost } from '@/utils/zipdownload'
  // 引入Form页面
  import actModelForm from './actModelForm.vue'
  import actModelMsgTemplateForm from './actModelMsgTemplateForm.vue'
  import { getToken } from '@/utils/auth'

  export default {
    components: {
      actModelForm,
      actModelMsgTemplateForm
    },
    data() {
      return {
        actModelData: null,
        designFormVisible: false,
        // 复制流程
        copyModel: false,
        // 遮罩层
        loading: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 工作流模型数据
        actModelList: [],
        // 是否显示弹出层
        open: false,
        actModelMsgTemplateOpen: false,
        // 模型类型字典
        categoryOptions: [],
        // 是否禁用导出
        canExport: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          place: undefined,
          category: undefined,
          modelIds: []
        },
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: { Authorization: 'Bearer ' + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + '/act/model/importData'
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      }
    },
    created() {
      this.getList()
      this.getDicts('act_model_type').then(response => {
        this.categoryOptions = response.data
      })
    },
    methods: {
      /** 查询模型列表 */
      getList() {
        this.loading = true
        listModel(this.queryParams).then(response => {
          this.actModelList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 流程设计
      designHandle(row) {
        const routeData = this.$router.resolve({ path: '/actModelDesign', query: { id: row.id }})
        window.open(routeData.href, '_blank')
      },
      categoryFormat(row) {
        return this.selectDictLabel(this.categoryOptions, row.category)
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          place: undefined,
          phone: undefined,
          largestNumber: undefined,
          callMeeting: '0',
          videoMeeting: '0',
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          delFlag: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
      },
      /** 新增按钮操作 */
      addHandle() {
        this.reset()
        this.open = true
        this.actModelData = this.form
      },
      /** 修改按钮操作 */
      updateHandle(row, copyModel) {
        this.$nextTick(() => {
          this.reset()
          const id = row.id || this.ids
          getModel(id).then(response => {
            this.actModelData = response.data
            this.open = true
            this.copyModel = copyModel
          })
        })
      },
      /** 删除按钮操作 */
      deleteHandle(row) {
        const ids = row.id || this.ids
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          return delModel(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {})
      },
      // 部署流程
      deployHandle(row) {
        this.$confirm(this.$t('是否确认部署?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        }).then(function() {
          return deployModel(row.id)
        }).then(() => {
          this.getList()
          this.msgSuccess(this.$t('部署成功'))
        }).catch(function() {})
      },
      /** 导出按钮操作 */
      exportHandle() {
        downLoadFilePost('/act/model/export', this.queryParams)
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.queryParams.modelIds = selection.map(item => item.id)
        this.canExport = !selection.length
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = this.$t('导入')
        this.upload.open = true
      },
      // 文件上传中处理
      handleFileUploadProgress() {
        this.upload.isUploading = true
      },
      // 文件上传成功处理
      handleFileSuccess(response) {
        this.upload.open = false
        this.upload.isUploading = false
        this.$refs.upload.clearFiles()
        this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
        this.getList()
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit()
      },
      msgTemplateHandle(row) {
        this.actModelMsgTemplateOpen = true
        this.$nextTick(() => {
          const data = {
            modelId: row.id
          }
          // 调用form页面的 init 方法初始化数据
          this.$refs.actModelMsgTemplateForm.init(data)
        })
      }
    }
  }
</script>
